package com.lagou.mr.test;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 自定义一个结构体
 * num，从文件中读取
 * rank，在map排序后，reduce写文件前，进行赋值
 */
public class RankBean implements WritableComparable<RankBean> {

    public int get_num() {
        return _num;
    }

    public void set_num(int _num) {
        this._num = _num;
    }
    //字面值
    private int _num;

    //reduce前借着compareTo的排序，传入rank，组装成完整的内容
    public void set_rank(int rank) {
        this._rank = rank;
    }
    //排名
    private int _rank;

    public RankBean() {
    }

    //map阶段生成bean，该阶段只能确定值，而随着其他值读入，rank可能会浮动变化，因此此时还不能确定rank
    public RankBean(int num) {
        this._num = num;
    }


    @Override
    public String toString() {
        return this._rank + "\t" + this._num;
    }

    //排序，根据字面值排序
    @Override
    public int compareTo(RankBean o) {
        if(this._num < o._num)
            return -1;
        else if(this._num > o._num)
            return 1;
        return 0;
    }
    //序列化
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this._num);
    }
    //反序列化
    @Override
    public void readFields(DataInput in) throws IOException {
        this._num = in.readInt();
    }
}
